package com.jic.market.controller.manage;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.market.api.manage.CouponManangeApi;
import com.jic.market.entity.MarketCouponPeriodEntity;
import com.jic.market.entity.base.BaseActivity;
import com.jic.market.request.*;
import com.jic.market.request.after.TellAddAllocationRequest;
import com.jic.market.request.manager.base.MerchantDeleteRequest;
import com.jic.market.request.manager.base.PromotionIdRequest;
import com.jic.market.request.manager.base.StoreDeleteRequest;
import com.jic.market.request.manager.coupon.*;
import com.jic.market.request.rest.coupon.SendCouponRequest;
import com.jic.market.response.CouponConfigQueryResponse;
import com.jic.market.response.CouponDetailResponse;
import com.jic.market.response.CouponNumAndGrouponNumResponse;
import com.jic.market.response.manager.coupon.*;
import com.jic.market.service.allocation.AllocationLogicService;
import com.jic.market.service.base.BaseActivityService;
import com.jic.market.service.coupon.CouponConfigService;
import com.jic.market.service.coupon.CouponGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * @author: gaolanyu
 * @date: 2020-01-09
 * @remark: 优惠券后台管理系统的服务    controller只做服务组装
 */
@RestController
@RequestMapping("/coupon")
@Api(tags = {"优惠券后台管理系统"})
@Slf4j
public class CouponManageController implements CouponManangeApi {

    @Resource
    private CouponConfigService couponConfigService;
    @Autowired
    CouponGoodsService couponGoodsService;
    @Autowired
    AllocationLogicService allocationLogicService;
    @Autowired
    BaseActivityService baseActivityService;

    @ApiOperation(value = "分页查询优惠券明细")
    @Override
    public RestResult<PageResult<CouponDetailResponse>> queryCouponDetail(@RequestBody CouponDetailRequest couponDetailRequest) {
        log.info("分页查询优惠券明细，入参{}", JSON.toJSONString(couponDetailRequest));
        RestResult<PageResult<CouponDetailResponse>> response = couponConfigService.queryCouponDetail(couponDetailRequest);
        log.info("分页查询优惠券明细，出参{}", JSON.toJSONString(response));
        return response;
    }

    @Override
    @ApiOperation(value = "删除优惠券明细")
    public RestResult<Boolean> deleteCouponDetail(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.deleteCouponDetail(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "分页查询优惠券配置列表")
    public RestResult<PageResult<CouponConfigQueryResponse>> queryCouponConfigPage(CouponConfigQueryRequest couponConfigQueryRequest) {
        log.info("分页查询优惠券活动，入参{}", JSON.toJSONString(couponConfigQueryRequest));
        RestResult<PageResult<CouponConfigQueryResponse>> response = couponConfigService.queryCouponConfigPage(couponConfigQueryRequest);
        log.info("分页查询优惠券活动，出参{}", JSON.toJSONString(response));
        return response;
    }

    @Override
    @ApiOperation(value = "根据活动Id查询活动配置")
    public RestResult<CouponConfigQueryResponse> queryCouponConfig(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.queryCouponConfig(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "优惠券活动列表添加配置")
    public RestResult<String> addCouponConfig(@RequestBody CouponConfigAddRequest couponConfigAddRequest) {
        return couponConfigService.addCouponConfig(couponConfigAddRequest);
    }

    @Override
    @ApiOperation(value = "优惠券活动列表更新配置")
    public RestResult<Boolean> updateCouponConfig(@RequestBody CouponConfigUpdateRequest couponConfigUpdateRequest) {
        return couponConfigService.updateCouponConfig(couponConfigUpdateRequest);
    }

    @Override
    @ApiOperation(value = "复制优惠券配置")
    public RestResult<String> copyCouponConfig(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.copyCouponConfig(promotionIdRequest, false);
    }

    @Override
    @ApiOperation(value = "关闭优惠券配置")
    public RestResult<Boolean> closeCouponConfig(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.closeCouponConfig(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "删除优惠券配置")
    public RestResult<Boolean> deleteCouponConfig(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.deleteCouponConfig(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "增加商家配置")
    public RestResult<Boolean> addCouponMerchantConfig(CouponMerchantAddRequest couponMerchantAddRequest) {
        return couponConfigService.addCouponMerchantConfig(couponMerchantAddRequest);
    }

    @Override
    @ApiOperation(value = "查询商家配置")
    public RestResult<CouponMerchantResponse> queryCouponMerchant(CouponDetailRequest promotionIdRequest) {
        return couponConfigService.queryCouponMerchant(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "删除商家配置")
    public RestResult<Boolean> deleteCouponMerchant(MerchantDeleteRequest merchantDeleteRequest) {
        return couponConfigService.deleteCouponMerchant(merchantDeleteRequest);
    }

    @Override
    @ApiOperation(value = "增加店铺配置")
    public RestResult<Boolean> addCouponStoreConfig(CouponStoreAddRequest couponStoreAddRequest) {
        return couponConfigService.addCouponStoreConfig(couponStoreAddRequest);
    }

    @Override
    @ApiOperation(value = "查询店铺配置")
    public RestResult<CouponStoreResponse> queryCouponStore(CouponDetailRequest promotionIdRequest) {
        return couponConfigService.queryCouponStore(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "删除店铺配置")
    public RestResult<Boolean> deleteCouponStore(StoreDeleteRequest storeDeleteRequest) {
        return couponConfigService.deleteCouponStore(storeDeleteRequest);
    }


    @ApiOperation(value = "审核配置")
    @Override
    @PostMapping("/check-coupon")
    public RestResult<Boolean> checkCoupon(@RequestBody CouponCheckRequest couponCheckRequest) {
        RestResult<Boolean> restResult = couponConfigService.checkCoupon(couponCheckRequest);
        if (RestResult.checkIsSuccess(restResult) && couponCheckRequest.getUpdateStatus().equals("11")) {
            TellAddAllocationRequest tellRequest = new TellAddAllocationRequest();
            BaseActivity activity = baseActivityService.queryActivityByPromotionId(couponCheckRequest.getPromotionId());
            if (activity == null) {
                return restResult;
            }
//            tellRequest.setPromotionId(activity.getPromotionId());
//            tellRequest.setPromType(Integer.parseInt(activity.getPromType()));
//            allocationLogicService.addAllocation(tellRequest);
        }
        return restResult;
    }

    @Override
    @ApiOperation(value = "保存模版")
    public RestResult<String> saveTemplate(CouponTemplatRequest couponTemplatRequest) {
        return couponConfigService.saveTemplate(couponTemplatRequest);
    }

    @Override
    @ApiOperation(value = "优惠券统计")
    public RestResult<PageResult<CouponCountResponse>> queryCouponCount(CouponCountRequest couponCountRequest) {
        RestResult<PageResult<CouponCountResponse>> pageResultRestResult = couponConfigService.queryCouponCount(couponCountRequest);
        return pageResultRestResult;
    }

    @Override
    @ApiOperation(value = "增加商品配置")
    public RestResult<Boolean> addGoodsLimit(CouponGoodsLimitIdsAddRequest couponGoodsLimitAddRequest) {
        return couponConfigService.addGoodsLimit(couponGoodsLimitAddRequest);
    }


    @Override
    @ApiOperation(value = "所有商品配置")
    public RestResult<Boolean> addAllGoods(CouponGoodsLimitIdsAddRequest couponGoodsLimitAddRequest) {
        return couponConfigService.addAllGoods(couponGoodsLimitAddRequest);
    }

    @Override
    @ApiOperation(value = "更新商品配置")
    public RestResult<Boolean> updateGoodsLimit(CouponGoodsLimitAddRequest couponGoodsLimitAddRequest) {
        return couponConfigService.updateGoodsLimit(couponGoodsLimitAddRequest);
    }

    @Override
    @ApiOperation(value = "查询商品配置")
    public RestResult<CouponGoodsQueryResponse> queryGoodsId(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.queryGoodsId(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "查询红包商品配置")
    public RestResult<CouponGoodsQueryResponse> queryRedGoodsByPromotionId(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.queryRedGoodsByPromotionId(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "查看红包互斥详情")
    public RestResult<PageResult<CouponConfigQueryResponse>> queryRedMutexDetailPromotionList(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.queryRedMutexDetailPromotionList(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "删除商品配置")
    public RestResult<Boolean> deleteCouponGoods(CouponGoodsDeleteRequest couponGoodsDeleteRequest) {
        return couponConfigService.deleteCouponGoods(couponGoodsDeleteRequest);
    }

    @Override
    @ApiOperation(value = "批量删除商品配置")
    public RestResult<Boolean> deleteBatchGoods(CouponBatchDeleteRequest couponBatchDeleteRequest) {
        return couponConfigService.deleteBatchGoods(couponBatchDeleteRequest);
    }

    @Override
    @ApiOperation(value = "增加触发场景")
    public RestResult<Boolean> addTriggerLimit(CouponTriggerLimitAddRequest couponTriggerLimitAddRequest) {
        return couponConfigService.addTriggerLimit(couponTriggerLimitAddRequest);
    }

    @Override
    @ApiOperation(value = "更新触发场景")
    public RestResult<Boolean> updateTriggerLimit(CouponTriggerLimitAddRequest couponTriggerLimitAddRequest) {
        return couponConfigService.updateTriggerLimit(couponTriggerLimitAddRequest);
    }

    @ApiOperation(value = "查询触发场景")
    @Override
    public RestResult<CouponTriggerQueryResponse> queryTrigger(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.queryTrigger(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "发券")
    public RestResult<Boolean> grantCoupon(CouponGrantRequest couponGrantRequest) {
        return couponConfigService.grantCoupon(couponGrantRequest);
    }

    @Override
    @ApiOperation(value = "根据活动Id查询全部商家Id")
    public RestResult<List<Long>> getMerchantIdsByPromotionId(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.getMerchantIdsByPromotionId(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "根据活动Id查询全部店铺Id")
    public RestResult<List<Long>> getStoreIdsByPromotionId(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.getStoreIdsByPromotionId(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "根据活动Id查询优惠券全部店铺Id")
    public RestResult<List<Long>> getCouponStoreIdsByPromotionId(PromotionIdRequest promotionIdRequest) {
        return couponConfigService.getCouponStoreIdsByPromotionId(promotionIdRequest);
    }

    @Override
    @ApiOperation(value = "重新添加红包")
    public RestResult<Map<String, Object>> rendRedAdd(CouponGoodsLimitAddRequest couponGoodsLimitAddRequest) {
        return couponConfigService.rendRedAdd(couponGoodsLimitAddRequest);
    }

    @Override
    @ApiOperation("查询红包列表")
    public RestResult<PageResult<CouponConfigQueryResponse>> getCouponList(CouponConfigQueryRequest couponConfigQueryRequest){
        return RestResult.success(couponConfigService.getCouponList(couponConfigQueryRequest));
    }

    @Override
    @ApiOperation("根据id查询红包")
    public RestResult<CouponConfigQueryResponse> getCouponByPromotionId(CouponConfigQueryRequest couponConfigQueryRequest){
        return RestResult.success(couponConfigService.getCouponByPromotionId(couponConfigQueryRequest));
    }

    @Override
    @ApiOperation("根据手机号发券")
    public RestResult<Long> backstageSendCoupon(BackstageSendCouponRequest request){
        return couponConfigService.backstageSendCoupon(request);
    }

    @Override
    @ApiOperation("根据批次id查询批次状态")
    public RestResult<Integer> queryPeriodStatusById(QueryPeriodStatusByIdRequest request) {
        return couponConfigService.queryPeriodStatusById(request);
    }

    @Override
    public RestResult<Boolean> sendCoupon(SendCouponRequest couponReceiveOfPersonRequest) {
        return couponConfigService.sendCoupon(couponReceiveOfPersonRequest);
    }

    @Override
    public RestResult<CouponNumAndGrouponNumResponse> queryMyCouponNumberAndGrouponNumber(CouponNumAndGrouponNumRequest request) {
        return couponConfigService.queryMyCouponNumberAndGrouponNumber(request);
    }

    @PostMapping("/queryCouponPeriod")
    @ApiOperation("查询发券记录")
    public RestResult<PageResult<MarketCouponPeriodEntity>> queryCouponPeriod(@RequestBody PromotionIdRequest request){
        return couponConfigService.queryCouponPeriod(request);
    }

    @PostMapping("/getCouponIssuableTotal")
    @ApiOperation("查询发券记录")
    public RestResult<Long> getCouponIssuableTotal(@RequestBody PromotionIdRequest request){
        return couponConfigService.getCouponIssuableTotal(request);
    }

}
